<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SvnBackup
 *
 * @author kevinthant
 */
namespace Backuper\Services;
use \Backuper\Settings\Svn;


class SvnBackup {
    //put your code here
    private $_containerName = null;
    public function __construct(){
        $this->_containerName = Svn::getInstance()->container;
    }
    
    public function backup(){
 
        foreach(Svn::getInstance()->repos as $repo){
            $this->backupRepository($repo);
        }
    
    }
    
    public function backupRepository($name){
        
        $svnadmin = Svn::getInstance()->clientPath;
        $repositoryUrl = Svn::getInstance()->rootPath . DIRECTORY_SEPARATOR . $name;
        $filename = TEMP_DIR . DIRECTORY_SEPARATOR . $name . '.dump';
        
        $output = array();
        $return = 0;
        exec('"'. $svnadmin . '"' .' dump -q ' . $repositoryUrl .' > ' .$filename, $output, $return);
        
        if($return != 0){
            throw new \Exception('Fail to dump the SVN repository: '. $name . implode("\n", $output));
        }
        
        if(!file_exists($filename)){
            throw new \Exception('Fail to dump the SVN repository: '. $name);
        }
        
        echo "Backup is dumped into ". $filename;
        
        CloudFiles::getInstance()->store(Svn::getInstance()->container, $filename);
       
    }
    
    public function unbackupRepository($name){
        
    }
    
    
}


